package com.szj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.media.sound.SoftAudioPusher;
import com.szj.entity.Admin;
import com.szj.service.AdminService;
import com.szj.util.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    private Map<String,Object> result=new HashMap<String,Object>();
    /**
     * 跳转到管理员页面
     * @return
     */
    @GetMapping("/goAdminListView")
    public String getAdminListView(){
        return "admin/adminList";
    }

    /**
     * 分页查询所有用户或者指定用户
     * @param page
     * @param rows
     * @param username
     * @return
     */
    @ResponseBody
    @PostMapping("/getAdminList")
    public Map<String, Object> getAdminList(Integer page, Integer rows, String username) {
        //获取查询用户名
        Admin admin = new Admin();
        admin.setName(username);
        //设置每页记录数
        PageHelper.startPage(page, rows);
        //根据用户名查询所有或者指定用户列表
        List<Admin> list = adminService.selectList(admin);
        //封装查询结果集
        PageInfo<Admin> pageInfo = new PageInfo<>(list);
        //获取总记录数
        long total = pageInfo.getTotal();
        //获取当前页用户列表
        List<Admin> adminList = pageInfo.getList();
        //封装数据到结果集
        result.put("total",total);
        result.put("rows",adminList);
        return result;
    }

    /**
     * 删除管理员
     * @return
     */
    @PostMapping("deleteAdmin")
    @ResponseBody
    public Map<String,Object> deleteAdmin(@RequestParam("ids[]") Integer[] ids) {
        if (adminService.adminDel(ids)>0){
            result.put("success",true);
        }
        else{
            result.put("success",false);
        }
        return result;
    }

    /**
     * 修改管理员 ???
     * @return
     */
    @PostMapping("editAdmin")
    @ResponseBody
    public Map<String,Object> editAdmin(Admin admin) {
        if (adminService.adminUP(admin)>0){
            result.put("success",true);
        }
        else{
            result.put("success",false);
        }
        return result;
    }

    /**
     * 添加管理员
     * @param admin
     * @return
     */
    @PostMapping("/addAdmin")
    @ResponseBody
    public Map<String,Object> addAdmin(Admin admin) {
        System.out.println(path);
        admin.setPortrait_path(path);
        System.out.println(admin);

        if (adminService.findAdminName(admin.getName())==null){
            if (adminService.addAdmin(admin)>0){
                result.put("success",true);
            }
            else{
                result.put("success",false);
            }
        }else {
            result.put("success",false);
            result.put("msg","用户名已存在，请重试！");
        }
        path="";
        return result;
    }

    /**
     * 上传头像,原理:将头像上传到项目发布目录中,用过读取数据库中的头像路径来获取头像
     *
     * @param photo
     * @param request
     * @return path=portraitPath;
     */
    @PostMapping("/uploadPhoto")
    @ResponseBody
    public Map<String, Object> uploadPhoto(MultipartFile photo, HttpServletRequest request) {
        //存储头像到本地目录
        final String dirpath = request.getServletContext().getRealPath("/upload/admin_portrait/");
        //System.out.println(dirpath);
        //获取图片的原始名称
        String orginalName = photo.getOriginalFilename();;
        //存储头像到项目发布目录
        final String portraitPath = request.getServletContext().getContextPath() + "/upload/admin_portrait/";
        //System.out.println();
        path=portraitPath+orginalName;
        //返回头像上传结果
        return UploadFile.getUploadResult(photo, dirpath, portraitPath);
    }
     String path="";
}